<?php
namespace Shop\Controller;
use Common\Controller\FrontbaseController;
/**
 * 店的各种API
 * @author Hongwei 2017-1-20
 */
class ShopController extends FrontbaseController{

	protected $shop_model;
	
	public function _initialize() {
		parent::_initialize();
		$this ->shop_model = D('Shop');
	}

	/**
	 * 根据 wifi 获取店铺的各种信息
	 * @return [json] [状态]
	 */
	public function getShopByWifi() {
		$shop_wifi = I("shop_wifi");
		$result = $this->shop_model->where("shop_wifi = '$shop_wifi'")->find();
		
		if ($result) {
			$assign['status'] = 1;
			$assign['msg']  = '成功';
			$assign['info'] = $result;
		} else {
			$assign['error'] = 1;
			$assign['error_msg'] = '没有数据';
		}
		$this -> output($assign);
	}


	/**
	 * 选择桌号
	 */
	public function selectTableNumber(){
		$shop_id = I('shop_id');
		$where['shop_id'] = $shop_id;
		$table_number_list = $this->shop_model->where($where)->find();
		

		$table_number = $table_number_list['shop_table_number'];
		$table_number_arr[] = explode( ',',$table_number);

		$table_number_list['table_list'] = explode( ',',$table_number);
		if ($table_number_list) {
			$assign['status'] = 1;
			$assign['msg']  = '成功';
			$assign['info'] = $table_number_list;
		} else {
			$assign['error'] = 1;
			$assign['error_msg'] = '没有数据';
		}
		$this -> output($assign);

	}

	/**
	 * 获取店铺内用户列表
	 * @author	Baiyu
	 * @param	number	$shop_id	店铺ID,值是0时为获取当前店铺的用户列表
	 */
	public function getUserListInShop($shop_id = 0, $offset = 0, $limit = 0) {
		
		//声明变量
		$user_ids = array();
		$weibo_users = array();
		$users = array();
		$result = array();
		
		//处理店铺ID
		if ($shop_id === 0) {
			$shop_id = session('shop_id');
		}
		
		//获取数据
		if ($shop_id) {
			$shop_users = D('ShopUser') -> where(array('shop_id' => $shop_id)) -> limit($offset, $limit) -> select();
			
			if (!empty($shop_users)) {
				foreach ($shop_users as $shop_user) {
					$user_ids[] = $shop_user['user_id'];
				}
				$user_ids = array_unique($user_ids);
				
				if (!empty($user_ids)) {
					$user_id = \User\Lib\ImiLib::getCurrentUserId();
					$users = \User\Lib\ImiLib::getUserByIds($user_ids);
					$weibo_users = \Weibo\Lib\ImiLib::getUserByIds($user_ids, 0, 0, $user_id ? $user_id : FALSE);
					if (!empty($users)) {
						foreach ($users as $user) {
							$result[] = array_merge($user, $weibo_users[$user['id']]);
						}
					}
				}
			}
		}
		
		//组织输出数据
		if (!empty($result)) {
			$assign['status'] = 1;
			$assign['msg'] = '获取店内用户列表成功';
			$assign['data'] = $result;
		} else {
			$assign['error'] = 101;
			$assign['error_msg'] = '列表为空';
		}
		
		//输出
		$this -> output($assign);
	}
	
	/**
	 * 加入店铺用户列表根据Wifi名称
	 * @author	Baiyu
	 * @param	string	$ssid		微博名称
	 */
	public function joinToShopByWifi($ssid = '') {
		
		//必需为APP请求
		!IS_APP && exit();
		
		//声明变量,获取店铺数据
		$result = FALSE;
		$shop = $this -> shop_model -> where(array('shop_wifi' => $ssid)) -> find();
		
		//当前用户添加到店铺
		if ($shop) {
			$user_id = \User\Lib\ImiLib::getCurrentUserId();
			if ($user_id) {
				$result = D('ShopUser') -> add(array(
						'session_id' => session_id(),
						'shop_id' => $shop['shop_id'],
						'user_id' => $user_id,
				), array(), TRUE);
			}
		}
		
		//组织输出数据
		if ($result !== FALSE) {
			session('shop_id', $shop['shop_id']);
			session('shop', $shop);
			$assign['shop'] = $shop;
			$assign['shop_name'] = $shop['shop_name'];
			$assign['msg'] = '加入店内用户列表成功';
			$assign['status'] = 1;
		} else {
			$assign['error'] = 101;
			$assign['error_msg'] = '操作失败';
		}
		
		//输出
		$this -> output($assign);
	}
	
	public function leaveShop() {
		
		//必需为APP请求
		!IS_APP && exit();

		//声明变量
		$result = FALSE;
		
		//执行操作
		$result = D('ShopUser') -> where(array('session_id' => session_id())) -> delete();

		//组织输出数据
		if ($result !== FALSE) {
			session('shop_id', NULL);
			session('shop', NULL);
			$assign['msg'] = '从店内用户列表中删除成功';
			$assign['status'] = 1;
		} else {
			$assign['error'] = 101;
			$assign['error_msg'] = '操作失败';
		}
		
		//输出
		$this -> output($assign);
	}

}
